package  
{
	import flash.events.EventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.events.Event;
	import flash.text.TextField;
	//import flash.events.EventDispatcher;
	
	/**
	 * ...
	 * @author 
	 */
	public class Control extends EventDispatcher 
	{
		private static var _instance:Control;
		public var powerCount:int = 0;
		private var m_KeyCode:Array = [ -1, -1, -1, -1];
		private var myTimer:Timer;
		
		public static var STATE_STOP:int = 0;
		public static var STATE_BEHIT:int = 2;
		public static var STATE_DEAD:int = 3;
		public var bBehit:Boolean = false;//被攻击
		public var bDead:Boolean = false;//是否死亡
		public var playerCount:int = 3;//人数
		
		public function Control() 
		{
			_instance = this;
		}
		
		//获得Control类的单例
		public static function GetInstance():Control {
			if (_instance)
				return _instance;
			else 
				return new Control();
		}
		
		//重置各种监听器，场景的舞台添加KEY_DOON，KEY_UP监听器，设置一个定时器
		public function restAll():void {
			View.GetInstance().resetData();
			View.GetInstance().viewStage.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
			View.GetInstance().viewStage.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
			View.GetInstance().viewStage.stage.addEventListener("gameOver",gameOver);
			myTimer = new Timer(100,0);
			myTimer.addEventListener("timer",timerHandler);
			myTimer.start();
		}
		
		//响应键盘按下事件，记录按下的键的键值
		private function onKeyboardDown(e:KeyboardEvent):void {
			for(var i:int = 0;i < m_KeyCode.length;i++){
				if(m_KeyCode[i] == -1)
				{
					m_KeyCode[i] = e.keyCode;
					return;
				}
				else if (m_KeyCode[i] == e.keyCode)
					return;
			}
		}
		
		//响应键盘弹起事件，从数组从移除弹起的键的键值
		private function onKeyboardUp(e:KeyboardEvent):void {
			for(var i:int = 0; i < m_KeyCode.length;i++){
				if(m_KeyCode[i] == e.keyCode)
					m_KeyCode[i] = -1;
			}
		}
		
		//响应timer事件
		private function timerHandler(e:TimerEvent):void {
			
			//player中的保护，JUMP后变为false
			if (View.GetInstance().m_Player.bflag == true) {
				View.GetInstance().m_Player.Input(m_KeyCode);
			}
			View.GetInstance().m_Player.Logic();
			
			//玩家攻击时，调用下面函数;
			if (View.GetInstance().m_Player.bAttack == true)
			{
				HitTest();
			}//被攻击时调用函数
			if (View.GetInstance().m_Player.bLife == true)
			{
				IsBehit();
				
			}//血值为空时玩家死亡状态
			if (bDead == true)
			{
				var posX:int = View.GetInstance().m_Player.x;
				var posY:int = View.GetInstance().m_Player.y;
				playerCount--;
				if(playerCount == 0){
					View.GetInstance().viewStage.stage.dispatchEvent(new Event("gameOver"));
					return;
				}else {
					View.GetInstance().m_UI.resetUiLife();
					View.GetInstance().m_Player.Reset(posX, posY);
					bDead = false;
				}
			}
			
			for each (var monster in View.GetInstance().m_aMonsters)
			{
				monster.Logic(View.GetInstance().m_Player);
			}
			for each (var npc in View.GetInstance().m_aMyNPCs)
			{
				npc.Input(View.GetInstance().m_Player,m_KeyCode);
				npc.NPCLogic(View.GetInstance().m_Player);
			}
			
		
			//显示主角被攻击时掉血量
			/*var m_TextLeft:TextField = new TextField();
			var m_TextRight:TextField = new TextField();
			//判断主角面朝哪个方向
			var flag:int = getFlag();
			m_TextLeft = m_Scene.getTextLeft();
			m_TextRight = m_Scene.getTextRight();
			
			//开始时处于看不见
			m_TextLeft.visible = false;
			m_TextRight.visible = false;*/
			
			if(bBehit == true)
			{
				IsBehit();
				//var life:int = UI.getCurLife();
				//var MonsterAttack:int = m_Scene.getMonsetAttack();//受到怪物的攻击
			    //life = life - MonsterAttack;
				//flasg = 1面朝左，右边显示,其余右边显示
				/*if(flag == 1)
				{
					m_TextRight.text = String(MonsterAttack);
					m_TextRight.visible = true;
				}
			    else
				{
					m_TextLeft.text = String(MonsterAttack);
					m_TextLeft.visible = true;
					}*/
				
			   // T_Life.setCurLife(life);
				
			    /*if(life <= 0)
				{
					bDead = true;
					init();
				}*/
			}
			View.GetInstance().Logic(m_KeyCode);
		}
		
		//响应gameOver事件，移除各种监听器
		private function gameOver(e:Event):void{
			View.GetInstance().viewStage.stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKeyboardDown);
			View.GetInstance().viewStage.stage.removeEventListener(KeyboardEvent.KEY_UP,onKeyboardUp);
			View.GetInstance().viewStage.stage.removeEventListener("gameOver",gameOver);
			myTimer.stop();
		}
		
		//获取怪物的攻击值,获得随机攻击值
		public function getMonsetAttack():int
		{
			View.GetInstance().m_aMonsters[0].m_Attack = View.GetInstance().m_aMonsters[0].setRandom();
			return View.GetInstance().m_aMonsters[0].m_Attack;
		}
		
		//检测攻击
		protected function HitTest():void
		{
			for(var i:int = 0;i < View.GetInstance().m_aMonsters.length;i++)
			{	
				if (View.GetInstance().m_Player.attackBox.hitTestObject(View.GetInstance().m_aMonsters[i].bodyBox))
				{
					powerCount ++;
					View.GetInstance().m_aMonsters[i].setState(STATE_BEHIT);
					View.GetInstance().m_aMonsters[i].dispatchEvent(new Event("beHit"))
					View.GetInstance().m_UI.bAddPower = true;
					//if (powerCount == 10) {
						//View.GetInstance().m_UI.updatUiPower();
						//powerCount = 0;
					//}
				}
				View.GetInstance().m_Player.bAttack = false;
			}
		}

		//设定值，相当于dispatchEvent（好像是这么拼的）
		protected function IsBehit():void
		{
			bBehit = false;
			View.GetInstance().m_Player.bLife = false;
			if (View.GetInstance().m_UI.updatUiLife()) {
				bDead = true;
			}
		}

		//传标志，判断主角面朝哪个方向//获取玩家面向的方向
		/*public function getFlag():int
		{
			return View.GetInstance().m_Player.flag;
		}*/
	}

}